const { sharedMap, isExternalMap, adminStatusMap,computeStatusMap } = require('../utils/dictionary')

/**
 * 转换镜像列表数据
 * @param {*} images 南京云平台返回的镜像列表数据
 * @returns 前端表格需要展示的数据
 */
function transferImages(images) {
    return images.map((item, index) => {
        return {
            id: item.id,
            owner: item.owner_name,
            name: item.name,
            status: item.status,
            visibility: item.visibility,
            isProtected: item.is_protected,
            diskFormat: item.disk_format,
            size: item.size,
            createdAt: item.created_at,
        }
    })
}


/**
 * 转换网络列表数据
 * @param {*} data 南京云平台返回的网络列表数据
 * @returns 前端表格需要展示的数据
 */
function transferNetworks(data) {
    return data.map((item, index) => {
        return {
            name: item.name,
            subnet: item.subnets.toString() || '',
            shared: sharedMap[item.shared],
            isExternal: isExternalMap[item['router:external']],
            status: item.status,
            adminStatus: adminStatusMap[item.admin_state_up],
        }
    })
}

/**
 * 转换虚拟机列表数据
 * @param {*} data 南京云平台返回的虚拟机列表数据
 * @returns 前端表格需要展示的数据
 */
 function transferServers(data) {
    return data.map((item, index) => {
        console.log('虚拟机信息：',item);
        return {
            id: item.id,
            name: item.name,
            adress: "内网IP:"+item.private_v4+ (item.public_v4? '\r\n 公网IP:'+ item.public_v4:''), //private_v4是内网地址
            serverIsoName: item.image.name, 
            // serverIsoName: item.hostname, 
            flavor: item.flavor.original_name,
            status: computeStatusMap[item['OS-EXT-STS:vm_state']] || item['OS-EXT-STS:vm_state'],
        }
    })
}
module.exports = {
    transferImages,
    transferNetworks,
    transferServers,
}